"""
按周期计算收益率，简单方法
"""
class cross_line1:
    def __init__(self,line=1):
        self.line=line
        self.data=[]
        self.timekey = None
    def oncalc(self,d,timekey=None):
        rst=None
        if d is not None:
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>2:
                self.data.pop(0)
            if len(self.data)==2:
                if self.data[-1]>=self.line:
                    if self.data[-2]<self.line:
                        rst = 2
                    else:
                        rst=1
                else:
                    if self.data[-2] > self.line:
                        rst=-2
                    else:
                        rst=-1
        return rst

class cross_line2:
    def __init__(self,line0=0.1,line1=0.9,mode="right"):
        """
        Args:
            line0:
            line1:
            mode: right|left|zone
        """
        self.line0=line0
        self.line1=line1
        self.data=[]
        self.timekey = None
        self.oncalc_imp ={
            "right":self.oncalc_right,
            "left":self.oncalc_left,
            "zone":self.oncalc_zone
        }
        self.oncalc=self.oncalc_imp[mode]
    def oncalc_right(self,d,timekey=None):
        rst=0
        if d is not None:
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>2:
                self.data.pop(0)
            if len(self.data)==2:
                if self.data[-1]>=self.line1:
                    if self.data[-2]<self.line1:
                        rst = 2
                    else:
                        rst=1

                if self.data[-1]<=self.line0:
                    if self.data[-2] > self.line0:
                        rst=-2
                    else:
                        rst=-1
        return rst
    def oncalc_left(self,d,timekey=None):
        rst=0
        if d is not None:
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>2:
                self.data.pop(0)
            if len(self.data)==2:
                if self.data[-2]>=self.line1:
                    if self.data[-1]<self.line1:
                        rst = -2
                    else:
                        rst=-1

                if self.data[-2]<=self.line0:
                    if self.data[-1] > self.line0:
                        rst=2
                    else:
                        rst=1
        return rst
    def oncalc_zone(self,d,timekey=None):
        rst=0
        if d is not None:
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>2:
                self.data.pop(0)
            if len(self.data)==2:
                if self.data[-1]>=self.line1:
                    rst = 2
                if self.data[-1]<=self.line0:
                    rst=-2
        return rst



